<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

class AutogeneratorModelXml_elements extends JModelList
{
    public $oApplication;
    public function __construct($aConfig = array())
    {
        if (empty($aConfig['filter_fields']))
        {
            $aConfig['filter_fields'] = array(
                'id', 'axe.id',
                'xml_type_id', 'axe.xml_type_id',
                'xml_attribute_id', 'axe.xml_attribute_id',
                'custom', 'axe.custom',
                
                'xml_type_title', 'axt.title', 
                'xml_type_description', 'axt.description',
                
                'xml_attribute_title', 'axa.title',
                'xml_attribute_default', 'axa.default',
                'xml_attribute_description', 'axa.description'
            );
        }

        parent::__construct($aConfig);
        
        $this->oApplication = JFactory::getApplication();
    }

    protected function populateState($ordering = null, $direction = null)
    {
        $sLayout = $this->oApplication->input->get('layout');

        if ($sLayout)
        {
            $this->context .= '.' . $sLayout;
        }

        $sSearch = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
        
        $this->setState('filter.search', $sSearch);

        parent::populateState('axa.title', 'asc');
    }

    protected function getStoreId($iId = '')
    {
        $iId .= ':' . $this->getState('filter.search');

        return parent::getStoreId($iId);
    }

    public function getItems()
    {
        return parent::getItems();
    }

    protected function getListQuery()
    {
        $oDatabase = $this->getDbo();
        $oQuery = $oDatabase->getQuery(true);

        $oQuery->select($this->getState('list.select', '
            axe.id, 
            axe.xml_type_id, 
            axe.xml_attribute_id, 
            axe.custom, 
            
            axt.title AS xml_type_title, 
            axt.description AS xml_type_description, 
            
            axa.title AS xml_attribute_title,
            axa.default AS xml_attribute_default,
            axa.description AS xml_attribute_description'));
        
        $oQuery->from('#__auto_xml_element AS axe');
        
        $oQuery->join('LEFT', '#__auto_xml_type AS axt ON axt.id = axe.xml_type_id');
        $oQuery->join('LEFT', '#__auto_xml_attribute AS axa ON axa.id = axe.xml_attribute_id');
        
        $oQuery->where('axe.xml_type_id = ' . $this->oApplication->input->getInt('iXmlTypeId'));

        $sSearch = $this->getState('filter.search');
        if (!empty($sSearch))
        {
            $sSearch = $oDatabase->Quote('%' . $oDatabase->escape($sSearch, true) . '%');
            
            $oQuery->where('(
                axt.title LIKE ' . $sSearch . ' 
                OR axt.description LIKE ' . $sSearch . ' 
                OR axa.title LIKE ' . $sSearch . ' 
                OR axa.default LIKE ' . $sSearch . '
                OR axa.description LIKE ' . $sSearch . '
            )');
        }

        // For ordering.
        $orderCol = $this->state->get('list.ordering', 'axa.title');
        $orderDirn = $this->state->get('list.direction', 'asc');
        
        $oQuery->order($oDatabase->escape($orderCol . ' ' . $orderDirn));
        
        return $oQuery;
    }

    public function getXMLElements($iXMLTypeId)
    {
        $oDatabase = $this->getDbo();
        $oQuery = $oDatabase->getQuery(true);

        $oQuery->select('element.id, attribute.title, element.custom, attribute.default')
                ->from('#__auto_xml_element AS element')
                ->join('LEFT', '#__auto_xml_attribute AS attribute ON attribute.id = element.xml_attribute_id')
                ->where('element.xml_type_id = ' . (int) $iXMLTypeId)
                ->order('attribute.ordering ASC');
        
        $oDatabase->setQuery($oQuery);
        
        try
        {
            $aXMLElements = $oDatabase->loadObjectList();
        }
        catch (RuntimeException $e)
        {
            JError::raiseWarning(500, $e->getMessage);
        }
        
        return $aXMLElements;
    }
    
}
